package com.kmproject.haifu.mvp.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jess.arms.base.BaseActivity;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;
import com.jess.arms.widget.alertview.AlertView;
import com.jess.arms.widget.alertview.AlertViewOnItemClickListener;
import com.jess.arms.widget.autolayout.AutoToolbar;
import com.kmproject.haifu.R;
import com.kmproject.haifu.app.utils.AppUtil;
import com.kmproject.haifu.app.utils.PwdCheckUtil;
import com.kmproject.haifu.di.component.DaggerUserResetPwdComponent;
import com.kmproject.haifu.mvp.contract.UserResetPwdContract;
import com.kmproject.haifu.mvp.presenter.UserResetPwdPresenter;
import com.xw.repo.XEditText;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.jess.arms.utils.Preconditions.checkNotNull;


/**
 * ================================================
 * Description:
 * <p>
 * Created by MVPArmsTemplate on 06/23/2019 15:27
 * <a href="mailto:jess.yan.effort@gmail.com">Contact me</a>
 * <a href="https://github.com/JessYanCoding">Follow me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms">Star me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms/wiki">See me</a>
 * <a href="https://github.com/JessYanCoding/MVPArmsTemplate">模版请保持更新</a>
 * ================================================
 */
public class UserResetPwdActivity extends BaseActivity<UserResetPwdPresenter> implements UserResetPwdContract.View {

    @BindView(R.id.toolbar_back)
    RelativeLayout toolbarBack;
    @BindView(R.id.toolbar_title)
    TextView toolbarTitle;
    @BindView(R.id.back_right_icon)
    ImageView backRightIcon;
    @BindView(R.id.back_right_txt)
    TextView backRightTxt;
    @BindView(R.id.toolbar_right)
    RelativeLayout toolbarRight;
    @BindView(R.id.toolbar)
    AutoToolbar toolbar;
    @BindView(R.id.user_pwd_txt)
    XEditText userPwdTxt;
    @BindView(R.id.user_resetPwd_btn)
    TextView userResetPwdBtn;

    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        DaggerUserResetPwdComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .view(this)
                .build()
                .inject(this);
    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_user_reset_pwd; //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
      userResetPwdBtn.setText(getIntent().getStringExtra("type").equals("setPwd")?ArmsUtils.getString(this,R.string.user_regist):ArmsUtils.getString(this,R.string.user_resetpwd));

        userPwdTxt.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if(s.toString().contains(" "))
                {
                    setBtnStatus(false);
                    return;
                }
                if (s.length()>7)
                {
                    setBtnStatus(true);
                }else {
                    setBtnStatus(false);
                }
            }
        });
    }

    @Override
    public String reSetTitle() {
        return getIntent().getStringExtra("type").equals("setPwd")?ArmsUtils.getString(this,R.string.user_setpwd):ArmsUtils.getString(this,R.string.user_resetpwd);
    }

    @Override
    public void showLoading() {
      loadingDialog.show();
    }

    @Override
    public void hideLoading() {
       loadingDialog.dismiss();
    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        ArmsUtils.snackbarText(message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {
        finish();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick(R.id.user_resetPwd_btn)
    public void onClick() {
        if (!PwdCheckUtil.isLetterAndDigit(userPwdTxt.getText().toString().trim()))
        {
            showMessage(ArmsUtils.getString(this,R.string.user_setpwd_tip));
            return;
        }
       if (getIntent().getStringExtra("type").equals("setPwd")){
            //调用注册接口
           mPresenter.userRegist(userPwdTxt.getText().toString().trim(),getIntent().getStringExtra("mobile"),getIntent().getStringExtra("identifyCode"));
       return;
        }
        //调用重置密码接口
        mPresenter.resetPwd(userPwdTxt.getText().toString().trim(),getIntent().getStringExtra("mobile"),getIntent().getStringExtra("identifyCode"));

    }

    /**
     * 设置登录按钮状态
     * @param isClick
     */
    public void setBtnStatus(boolean isClick)
    {
        if (isClick)
        {
            userResetPwdBtn.setEnabled(true);
            userResetPwdBtn.setBackgroundResource(R.drawable.button_yellow_bg);
            userResetPwdBtn.setTextColor(getResources().getColor(R.color.color_191919));
        }else {
            userResetPwdBtn.setEnabled(false);
            userResetPwdBtn.setBackgroundResource(R.drawable.button_gray_bg);
            userResetPwdBtn.setTextColor(getResources().getColor(R.color.color_EDEDED));
        }
    }

    @Override
    public void userRegistSuccess() {
        Intent intent=new Intent();
        setResult(RESULT_OK,intent);
        killMyself();
    }

    @Override
    public void userRegistFail(String msg) {

        showDialog();
    }

    @Override
    public void resetPwdSuccess() {
        AppUtil.midToast(this,"密码修改成功");
        Intent intent=new Intent();
        setResult(RESULT_OK,intent);
        killMyself();
    }

    public void showDialog(){
        new AlertView("", ArmsUtils.getString(this,R.string.dilaog_titile_sjhyzc), ArmsUtils.getString(this,R.string.dilaog_titile_qx), new String[]{ArmsUtils.getString(this,R.string.dilaog_titile_qdl)}, null, this, AlertView.Style.Alert, new AlertViewOnItemClickListener() {

            @Override
            public void onItemClick(Object o, int position) {
                if (position == AlertView.CANCELPOSITION) {
                    //点击取消按钮

                }else {
                    Intent intent=new Intent();
                    setResult(RESULT_OK,intent);
                    killMyself();
                }
            }
        }).show();
    }
}
